package com.tianyuchan.exception.dto.resp;

import com.tianyuchan.exception.enums.CommonResponseCode;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一的公共响应体
 * @param <T>
 */
@Data
@AllArgsConstructor (access = AccessLevel.PROTECTED)
public class BaseResponse<T> implements Serializable {

    // 使用自动生成的随机long值作为版本序列号
    private static final long serialVersionUID = -4985150429002262656L;

    /**
     * 返回状态
     */
    private Boolean success;
    /**
     * 返回状态码
     */
    private String code;
    /**
     * 返回信息
     */
    private String msg;
    /**
     * 数据
     */
    private T data;

    protected BaseResponse() {
        this.setSuccess(true);
        this.setCode(CommonResponseCode.SUCCESS.name());
        this.setMsg(CommonResponseCode.SUCCESS.getMsg());
    }

    public static <T> BaseResponse<T> success() {
        return new BaseResponse<T>( Boolean.TRUE, CommonResponseCode.SUCCESS.name(), CommonResponseCode.SUCCESS.getMsg(), null);
    }

//    public static <T> BaseResponse<T> success(String message) {
//        return new BaseResponse<T>( Boolean.TRUE, CommonResponseCode.SUCCESS.name(), message, null);
//    }

    public static <T> BaseResponse<T> success(T data) {
        return new BaseResponse<T>( Boolean.TRUE, CommonResponseCode.SUCCESS.name(), CommonResponseCode.SUCCESS.getMsg(), data);
    }

    public static <T> BaseResponse<T> success(String message, T data) {
        return new BaseResponse<T>( Boolean.TRUE, CommonResponseCode.SUCCESS.name(), message, data);
    }

    public static <T> BaseResponse<T> fail() {
        return new BaseResponse<T>( Boolean.FALSE, CommonResponseCode.FAIL.name(), CommonResponseCode.FAIL.getMsg(), null);
    }

    public static <T> BaseResponse<T> fail(String message) {
        return new BaseResponse<T>( Boolean.FALSE, CommonResponseCode.FAIL.name(), message, null);
    }

//    public static <T> BaseResponse<T> fail(T data) {
//        return new BaseResponse<T>( Boolean.FALSE, CommonResponseCode.FAIL.name(), CommonResponseCode.FAIL.getMsg(), data);
//    }

    public static <T> BaseResponse<T> fail(String message, T data) {
        return new BaseResponse<T>( Boolean.FALSE, CommonResponseCode.FAIL.name(), message, data);
    }
}
